from datetime import datetime
from pydantic import BaseModel, EmailStr,Field
from typing import Optional, List


class UserBase(BaseModel):
    email: EmailStr
    nickname: str  # 用户名


class UserCreate(UserBase):
    password: str=Field(description="注册填写的密码")
    sex: int | None = None  # 性别 0-女 1-男 2-保密
    # 不需要Config，因为：
    # 1. 不从数据库模型转换
    # 2. 只做输入验证


class UserOut(UserBase):
    id:int
    userid: str
    avatar: str | None = None
    sex: int | None = None
    online: bool
    created_at: datetime
    last_at: datetime | None = None

    class Config:
        from_attributes = True
        validate_by_name = True
        extra = "ignore"

class UserResponse(BaseModel):
    code: int
    message: str
    data: UserOut  # 直接使用UserOut作为data字段的类型

class ApplyReviewRequest(BaseModel):
    id: int
    status: int  # 1-同意 2-拒绝
    remark: Optional[str] = None
    groupingId: Optional[int] = None

class FriendInfoResponse(BaseModel):
    id: int  # 好友关系ID
    remark: Optional[str]
    userId: int  # 好友的数字主键
    userBusinessId: str  # 好友的业务ID
    nickname: str
    avatar: Optional[str]
    sex: int
    online: bool
    email: Optional[str]
    lastAt: Optional[str]
    createdAt: str

class FriendGroupResponse(BaseModel):
    id: int
    name: str
    fixed: int
    friends: List[FriendInfoResponse]

class ApplyPageItem(BaseModel):
    id: int
    applicantId: int = Field(..., alias="applicant_id")  # 申请人数字主键
    applicantBusinessId: str = Field(..., alias="applicant_business_id")  # 申请人业务ID
    applicantName: str = Field(..., alias="applicant_name")
    targetId: int = Field(..., alias="target_id")  # 目标用户数字主键
    targetBusinessId: str = Field(..., alias="target_business_id")  # 目标用户业务ID
    targetName: str = Field(..., alias="target_name")
    content: Optional[str]
    status: int
    createdAt: str = Field(..., alias="created_at")
    online: int = Field(0)  # 添加online字段，默认为0

    class Config:
        validate_by_name = True  # 允许别名映射

class ApplyPageResponse(BaseModel):
    code: int = 0
    message: str = "success"
    data: List[ApplyPageItem]
    total: int

class GroupSelectResponse(BaseModel):
    id: int
    name: str

class GroupingFriendItem(BaseModel):
    id: int
    name: str
    fixed: int
    friends: List[dict]
